$(document).ready(function(){
	$(".image-status").live('click',function(){
		var key = $(this).parents('tr').attr('rel');
		var status = $(this).attr('status');
		var controller = $("#thetable").attr('controller');
		var page = $('#thetable').attr('page');
		$.ajax({
			type: "post",
			url: baseURL + 'administrator/' + controller + '/updatestatus' + page,
			data: {key: key, status: status},
			success: function(data){
				var result = $.parseJSON(data);
				$("#table_row_" + result.id).find('.image-status').attr('status',result.status);
				$("#table_row_" + result.id).find('.image-status').attr('src', baseURL + 'public/images/status_' + result.status + '.png');
				status = $("#table_row_" + result.id).find('.image-status').attr('status');
				if(status == "Disable"){
					$("#table_row_" + result.id).find('td a').css({opacity: "0.5"});
				}else{
					$("#table_row_" + result.id).find('td a').css({opacity: "1"});
				}
			}
		});
		
	});
	$(".image-status1").live('click',function(){
		var status = $(this).attr('status');
		var url = $(this).attr('url');
		$.ajax({
			type: "post",
			url: url,
			data: {status: status},
			success: function(data){
				var result = $.parseJSON(data);
				$("#table_row_" + result.id).find('.image-status1').attr('status',result.status);
				$("#table_row_" + result.id).find('.image-status1').attr('src', baseURL + 'public/images/status_' + result.status + '.png');
				status = $("#table_row_" + result.id).find('.image-status1').attr('status');
				if(status == "Disable"){
					$("#table_row_" + result.id).find('td a').css({opacity: "0.5"});
				}else{
					$("#table_row_" + result.id).find('td a').css({opacity: "1"});
				}
			}
		});
		
	});
	hiddenStatus();

	$("#xoa").live('click',function(){
		var checked = 0;
		$(".checkbox").each(function(){
			if($(this).is(':checked')){
				checked ++;
			}
		});
		if(checked==0){
			alert("Không có mục nào đươc chọn !!!");
		}else{
			if(confirm("Bạn có chắc muốn xóa nhưng dòng đã chọn?")){
				var success = 0;
				var time = 0;
				$(".checkbox").each(function(){
					if($(this).is(':checked')){
						var type = $("#thetable").attr('type');
						var key = $(this).parents('tr').attr('rel');
						var data;
						if(type == undefined){
							data = {key: key};
						}else{
							data = {key: key, type: type};
						}
						var controller = $("#thetable").attr('controller');
						var page = $('#thetable').attr('page');
						$.ajax({
							type: "post",
							url: baseURL + 'administrator/' + controller + '/delete' + page,
							data: data,
							success: function(data){
								$("#wrap_row_data").empty().append(data);
								onload();
							}
						});
						success ++;
						time = time + 100;

					}
					$(this).attr('checked', false);
					$("#checkAll").attr('checked', false);
				});
				
				if(success == 0){ 
					setTimeout(function(){
                		new Messi('<div id="error">KhÃ´ng cÃ³ má»¥c nÃ o Ä‘Æ°á»£c xÃ³a.</div>', {closeButton: false, autoclose: 3000});
					}, 500);
				}else{
					setTimeout(function(){
                		new Messi('<div id="success">CÃ³ '+ success +' dÃ²ng Ä‘Æ°á»£c xÃ³a thÃ nh cÃ´ng.</div>', {closeButton: false, autoclose: 3000});
					}, time);
				}
	
				
			}
		}
	});
	
	
});

function hiddenStatus(){
	$(".image-status").each(function(){
		var status = $(this).attr('status');
		if(status == "Disable"){
			$(this).parents('tr').find('td a').css({opacity: "0.5"});
		}
	});
}